package ipad.asia.controller;
import ipad.asia.dao.EnterpriseDao;
import ipad.asia.dao.PunishEnterpriseDao;
import ipad.asia.dao.ShopDao;
import ipad.asia.entity.Enterprise;
import ipad.asia.entity.PunishConsumer;
import ipad.asia.entity.PunishEnterprise;
import ipad.asia.entity.Shop;
import ipad.asia.service.EnterpriseService;
import ipad.asia.service.ShopService;
import ipad.asia.utils.MD5Util;
import ipad.asia.utils.MyTime;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;


@RestController
public class EnterpriseSignController {
    @Autowired
    private ShopService shopService;
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private EnterpriseDao enterpriseDao;
    @Autowired
    private ShopDao shopDao;
    @Autowired
    private PunishEnterpriseDao punishEnterpriseDao;
    @Autowired
    private MD5Util md5Util;
    @Autowired
    private MyTime myTime;
    String salt=md5Util.RandomSalt();
    /**
     * 登录说明：
     * 3是表示封号，禁止登录，跳转到申请解锁页面
     * 2表示严重警告！需要处理被举报的东西
     * 1表示举报，警告你不要再犯错误
     * 4表示可以正常登录
     * 0表示账号密码错误
     * @param user
     * @param password
     * @return
     * @throws IOException
     */

    @RequestMapping("/enterprise/login")
    public int login(@RequestParam(value = "user",required = false) String user,
                         @RequestParam(value = "password", required = false) String password) throws IOException {
        Enterprise enterprise = enterpriseService.findByUser(user);
        if (null != enterprise) {
           String newPW= MD5Util.FormPassToDbPass(password,enterprise.getSalt());
            if (newPW.equals(enterprise.getPassword())) {
                List<PunishEnterprise> punishEnterprises = punishEnterpriseDao.findByShopId(enterprise.getShopId());
                for (int i = 0; i < punishEnterprises.size(); i++) {
                    if (punishEnterprises.get(i).getSerious() == 3) {
                        return 3;
                    }
                    if (punishEnterprises.get(i).getSerious() == 2) {
                        return 2;
                    }
                    if (punishEnterprises.get(i).getSerious() == 1) {
                        return 1;
                    }
                }
                return 4;
            }
        }
        return 0;
    }
    /**
     * 直接注册
     * @param user
     * @param password
     * @param email
     * @param phone
     * @param
     * @return
     */
    @Transactional
    @RequestMapping ("/enterprise/register/")
    public boolean register(@RequestParam(value = "user",required = false) String user,
                            @RequestParam(value = "password", required = false) String password,
                            @RequestParam(value = "email",required = false) String email,
                            @RequestParam(value = "phone",required = false) String phone,
                            @RequestParam(value = "shopName",required = false) String shopName,
                            @RequestParam(value = "credentials",required = false) String credentials,
                            @RequestParam(value = "address",required = false) String address
          ) {
        if (user == null&&shopName==null&&password==null) {
            return false;
        } else {
            System.out.println("phone-------:"+phone);
            System.out.println("user--------:"+enterpriseService.findByUser(user));
            if (null==enterpriseService.findByUser(user)&&shopDao.findByName(shopName)==null) {
                //临时的商家id
                Shop shop=new Shop();
                shop.setEnterpriseId(99999);
                shop.setName(shopName);
                shop.setDate(myTime.LocalDateTime());
                shopDao.save(shop);

                Enterprise enterprise = new Enterprise();
                enterprise.setShopId(shopService.findByEnterpriseId(99999).getId());
                enterprise.setUser(user);
                enterprise.setAddress(address);
                enterprise.setCredentials(credentials);
//                String PW= md5Util.inputPassToFormPass(password);//这个是前端使用
                String md5PW=md5Util.FormPassToDbPass(password,salt);//这个是后端使用
                enterprise.setSalt(salt);
                enterprise.setUser(user).setEmail(email).setPhone(phone);
                enterprise.setPassword(md5PW);
                enterprise.setDate(myTime.LocalDateTime());
                enterpriseService.save(enterprise);
                System.out.println("enterprise:"+enterpriseService.findByUser(user).getId());
                System.out.println("shopname:"+shopDao.findByName(shopName));
                shopDao.updateEnterpriseId(enterpriseService.findByUser(user).getId(),shopName);
                System.out.println("ok-----------------------------");
                return true;
            }else {return false;}
        }
    }

    /**顾名思义，通过获取商家的信息,主要用作session和聊天*/
    @RequestMapping("/enterprise/get/{id}")
    public Enterprise get(@PathVariable("id") Integer id) throws Exception {
//        response.setCharacterEncoding("UTF-8");
        if(enterpriseService.findById(id)!=null){
        return enterpriseService.findById(id);}else {throw new Exception();}
    }


    @RequestMapping("/enterprise/findAll")
    public List<Enterprise> findAll(){
        return enterpriseDao.findAll();
    }
    /**
     * 更新个人信息
     * @param user
     * @param phone
     * @param email
     * @param sex
     * @param address
     * @param credentials
     * @param password
     */
    @Transactional
    @RequestMapping(value = "/enterprise/updateInfo")
    public void updateInfo( @RequestParam(value = "user",required = false)String user,
                            @RequestParam(value = "phone",required = false) String phone,
                            @RequestParam(value = "email",required = false) String email,
                            @RequestParam(value = "sex",required = false) String sex,
                            @RequestParam(value = "address",required = false) String address,
                            @RequestParam(value = "credentials",required = false) String credentials,
                            @RequestParam(value = "password",required = false) String password
                            ){
       String salt = enterpriseDao.findByUser(user).getSalt();
       String newPW=  MD5Util.FormPassToDbPass(password,salt);
        System.out.println("newPW"+newPW);
        enterpriseDao.updateInfo(user,phone,email,sex,address,credentials,newPW);
    }


    /**顾名思义，通过获取商家的信息*/
    @RequestMapping("/enterprise/getName")
    public Enterprise getName(@RequestParam(value = "user",required = false) String user) throws Exception {
        if(enterpriseService.findByUser(user)!=null){
            return enterpriseService.findByUser(user);}else {throw new Exception();}
    }

}
